﻿using System;
using System.Runtime.InteropServices;

namespace Pubg
{
    public class MouseMoveCommon
    {
        private struct Input
        {
            public SendInputEventType Type;

            public MouseKeybdhardwareInputUnion Mkhi;
        }
        [StructLayout(LayoutKind.Explicit)]
        private struct MouseKeybdhardwareInputUnion
        {
            [FieldOffset(0)] public MouseInputData Mi;

            [FieldOffset(0)] public Keybdinput Ki;

            [FieldOffset(0)] public Hardwareinput Hi;
        }
        private struct Keybdinput
        {
            public ushort wVk;

            public ushort wScan;

            public uint dwFlags;

            public uint time;

            public IntPtr dwExtraInfo;
        }
        private struct Hardwareinput
        {
            public int uMsg;

            public short wParamL;

            public short wParamH;
        }
        private struct MouseInputData
        {
            public int dx;

            public int dy;

            public uint mouseData;

            public MouseEventFlags dwFlags;

            public uint time;

            public IntPtr dwExtraInfo;
        }

        [Flags]
        private enum MouseEventFlags : uint
        {
            /// <summary>
            /// 移动
            /// </summary>
            MOUSEEVENTF_MOVE = 1u,
            /// <summary>
            /// 左键按下
            /// </summary>
            MOUSEEVENTF_LEFTDOWN = 2u,
            /// <summary>
            /// 左键弹起
            /// </summary>
            MOUSEEVENTF_LEFTUP = 4u,
            /// <summary>
            /// 右键按下
            /// </summary>
            MOUSEEVENTF_RIGHTDOWN = 8u,
            /// <summary>
            /// 右键弹起
            /// </summary>
            MOUSEEVENTF_RIGHTUP = 0x10,//16

            MOUSEEVENTF_MIDDLEDOWN = 0x20,//32
            MOUSEEVENTF_MIDDLEUP = 0x40,//64
            MOUSEEVENTF_XDOWN = 0x80,//128
            MOUSEEVENTF_XUP = 0x100,//512
            MOUSEEVENTF_WHEEL = 0x800,//1024
            MOUSEEVENTF_VIRTUALDESK = 0x4000,//2048
            MOUSEEVENTF_ABSOLUTE = 0x8000//4096
        }
        private enum SendInputEventType
        {
            InputMouse,
            InputKeyboard,
            InputHardware
        }
        [DllImport("user32.dll", SetLastError = true)]
        private static extern uint SendInput(uint nInputs, ref Input pInputs, int cbSize);
        /// <summary>
        /// 移动鼠标
        /// </summary>
        /// <param name="x">横向</param>
        /// <param name="y">纵向</param>
        public static void MouseMoves(int x, int y)
        {
            var iNput = default(Input);
            iNput.Type = SendInputEventType.InputMouse;
            iNput.Mkhi.Mi.dx = x;
            iNput.Mkhi.Mi.dy = y;
            iNput.Mkhi.Mi.dwFlags = MouseEventFlags.MOUSEEVENTF_MOVE;
            SendInput(1u, ref iNput, Marshal.SizeOf(default(Input)));
        }
        /// <summary>
        /// 点击鼠标
        /// </summary>
        /// <param name="x">横向坐标</param>
        /// <param name="y">纵向坐标</param>
        public static void MouseClicks(int x, int y)
        {
            var iNput = default(Input);
            iNput.Type = SendInputEventType.InputMouse;
            iNput.Mkhi.Mi.dwFlags = MouseEventFlags.MOUSEEVENTF_LEFTDOWN;
            SendInput(1u, ref iNput, Marshal.SizeOf(default(Input)));

            var iNput2 = default(Input);
            iNput2.Type = SendInputEventType.InputMouse;
            iNput2.Mkhi.Mi.dwFlags = MouseEventFlags.MOUSEEVENTF_LEFTUP;
            SendInput(1u, ref iNput2, Marshal.SizeOf(default(Input)));
        }
    }
}
